package com.project.template.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.template.common.PageVO;
import com.project.template.common.Result;
import com.project.template.dto.LoginDTO;
import com.project.template.dto.UserDTO;
import com.project.template.dto.UserLoginDTO;
import com.project.template.dto.UserRegisterDTO;
import com.project.template.entity.Product;
import com.project.template.entity.User;
import com.project.template.service.UserService;
import com.project.template.utils.JwtUtils;
import com.project.template.utils.Utils;
import com.project.template.utils.userUtils;
import com.project.template.vo.SysUserVO;
import com.project.template.vo.UserVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;



@RestController
@RequestMapping("/user")
public class UserController {


    @Resource
    private UserService userService;

    /**
     * 列表
     *
     * @return
     */
    @ApiOperation(value = "用户列表", notes = "")
    @GetMapping("/list")
    public Result<List<User>> list() {
        return new Result<>().success(userService.list());
    }

    /**
     * 分页查询
     *
     * @param query
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    public Result<PageVO<User>> findPage(@RequestParam Map<String, Object>
      query, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10")
    Integer pageSize) {
        Page<User> page = userService.page(query, pageNum, pageSize);
        return new Result<>().success(new PageVO<>(page));

    }

    /**
     * 数据新增
     *
     * @param user
     * @return
     */
    @ApiOperation(value = "数据新增", notes = "数据新增")
    @PostMapping()
    public Result add(@Validated @RequestBody User user) {
        userService.save(user);
        return new Result<>().success();
    }

    /**
     * 数据更新
     *
     * @param user
     * @return
     */
    @ApiOperation(value = "数据更新", notes = "数据更新")
    @PutMapping()
    public Result updateById(@Validated @RequestBody User user) {
        userService.updateById(user);
        return new Result<>().success();
    }


    /**
     * 根据id删除
     *
     * @param ids
     * @return
     */
    @ApiOperation(value = "数据根据id批量删除", notes = "数据根据id批量删除")
    @DeleteMapping()
    public Result delBatch(@RequestBody List<Integer> ids) {
        userService.removeByIds(ids);
        return new Result<>().success();
    }


    /**
     * 用户登录
     *
     * @param user
     * @return
     */
    /**
     * 用户登录
     *

     * @return
     */
    @PostMapping("/login")
    public Result login(@Validated @RequestBody LoginDTO loginDTO) {
        return new Result<>().success(userService.login(loginDTO));
    }

    /**
     * 用户注册
     *
     * @param userDTO
     * @return
     */
    @PostMapping("/register")
    public Result register(@Validated @RequestBody UserDTO userDTO) {
        userService.register(userDTO);
        return new Result<>().success();
    }
    /**
     * 根据id查询
     *
     * @param id
     * @return
     */

    @GetMapping("/getById")
    public Result<UserVO> getById(@RequestParam("id") Integer id) {
      User byId = userService.getById(id);
       UserVO userVO = new UserVO();
        BeanUtils.copyProperties(byId, userVO);
        return new Result<>().success(userVO);
    }

    @GetMapping("/queryUserInfo")
    public Result queryUserInfo() {

        return new Result<>().success(userService.getById(Utils.getuser().getId()));
    }

    @PutMapping("/updateUserInfo")
    public Result updateUserInfo(@Validated @RequestBody User user) {
        boolean result = userService.updateById(user);
        if (result) {
            return new Result<>().success();
        } else {
            return new Result<>().error("更新失败");
        }
    }

    @PutMapping("/changePassword")
    public Result changePassword(@RequestBody Map<String, String> passwordInfo) {
        String oldPassword = passwordInfo.get("oldPassword");
        String newPassword = passwordInfo.get("newPassword");

        Integer userId = Utils.getuser().getId(); // 假设你有一个方法获取当前登录用户的ID

        userService.changePassword(userId, oldPassword, newPassword);

        return new Result<>().success("密码修改成功");
    }



}
